﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lost_and_Found.Models.DB;
using Lost_and_Found.Models.ObjectManager;
using Lost_and_Found.Models.ViewModel;
using System.Web.Security;

namespace Lost_and_Found.Controllers
{
    [Authorize(Roles = "Admin")]
    public class AdministratorController : Controller
    {
        //
        // GET: /Administrator/
        LAFEntities db = new LAFEntities();
        Lost_and_Found.Models.ObjectManager.ItemManager itemManager = new Lost_and_Found.Models.ObjectManager.ItemManager();

        public ActionResult Index()
        {

            return View();
        }
        //============================= ItemManager ======================================

        public ActionResult Users()
        {
            List<User> listUser = new List<User>();
            listUser = db.Users.Where(m => m.Roles != "Member").ToList();
            return View(listUser);
        }

        public ActionResult NewUser()
        {
            return View();
        }

        [HttpPost]
        public ActionResult NewUser(AdminModel user)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    UserManager userManager = new UserManager();
                    if (!userManager.IsUserLoginIDExist(user.Username))
                    {
                        userManager.Add(user);
                        return RedirectToAction("Users", "Administrator");
                    }
                    else
                    {
                        ModelState.AddModelError("", "Đã tồn tại tên đăng nhập này");
                    }
                }
            }
            catch
            {
                return View(user);
            }
            return View(user);
        }

        [HttpGet]
        public ActionResult EditUser(int id)
        {
            User user = db.Users.Find(id);
            if (user != null)
            {
                UserEditModel u = new UserEditModel();
                u.Username = user.Username;
                user.Fullname = user.Fullname;
                u.Email = user.Email;
                u.Phone = user.Phone;
                u.SClass = user.SClass;
                u.SIndex = user.SIndex;
                u.SInstitute = user.SInstitute;
                u.Role = user.Roles;
                return View(u);
            }
            else
                return View();
        }

        [HttpGet]
        public ActionResult DeleteUser(int id)
        {
            User user = db.Users.Find(id);
            return View(user);
        }

        [HttpPost]
        public ActionResult DeleteUser(string username)
        {
            User user = (User)db.Users.Single(m => m.Username.Equals(username));
            if (!username.Equals(User.Identity.Name))
            {
                db.Users.Remove(user);
                db.SaveChanges();
                return RedirectToAction("Users", "Administrator");
            }
            return View(user);

        }

        //============================= ItemManager ======================================
        public ActionResult ItemManager()
        {
            List<ItemSuggestModel> list = new List<ItemSuggestModel>();
            var listItem = db.ItemsSuggest.ToList().OrderBy(a => a.Name);
            foreach (var item in listItem)
            {
                list.Add(new ItemSuggestModel { Name = item.Name, ListAttribute = itemManager.getListAttribute(item.ID), ID = item.ID });
            }
            return View(list);
        }
        //============================== AttributeManager=================================
        public ActionResult AttributeManager()
        {
            List<AttributeSuggest> list = new List<AttributeSuggest>();
            list = db.AttributeSuggest.ToList();
            return View(list);
        }
        //============================ Create ===========================================

        public ActionResult CreateItem()
        {

            return View();
        }

        [HttpPost]
        public ActionResult CreateItem(ItemSuggestModel item)
        {
            if (ModelState.IsValid)
            {
                var exitsItem = db.ItemsSuggest.Any(m => m.Name == item.Name);
                if (!exitsItem)
                {
                    itemManager.createSuggestItem(item);
                    return RedirectToAction("ItemManager");
                }
            }

            return View(item);
        }

        //=============================Edit Item======================================

        public ActionResult EditItem(int ItemId)
        {
            ItemSuggest item = db.ItemsSuggest.Find(ItemId);
            if (item == null)
            {
                return HttpNotFound();
            }
            string attribute = itemManager.getAttribute(ItemId);
            ItemSuggestModel itemSuggest = new ItemSuggestModel { ID = item.ID, Name = item.Name, Attribute = attribute };
            return View(itemSuggest);
        }
        [HttpPost]
        public ActionResult EditItem(ItemSuggestModel itemSuggest)
        {

            itemManager.editItem(itemSuggest.ID, itemSuggest.Attribute);
            return RedirectToAction("ItemManager");


        }


        //======================Delete Item ===========================================

        public ActionResult DeleteItem(int ItemId)
        {

            ItemSuggest item = db.ItemsSuggest.Find(ItemId);
            if (item == null)
            {
                return HttpNotFound();
            }
            ItemSuggestModel itemSuggest = new ItemSuggestModel { ID = item.ID, Name = item.Name, ListAttribute = itemManager.getListAttribute(ItemId) };
            return View(itemSuggest);

        }
        [HttpPost]
        public ActionResult DeleteItem(ItemSuggestModel itemSuggest)
        {
            if (ModelState.IsValid)
            {
                ItemSuggest item = db.ItemsSuggest.Find(itemSuggest.ID);
                if (item == null)
                {
                    return HttpNotFound();
                }
                itemManager.deleteAllAttributeOfItem(item.ID);
                db.ItemsSuggest.Remove(item);
                db.SaveChanges();
                return RedirectToAction("ItemManager");
            }
            else
            {
                return View(itemSuggest);
            }


        }

        //================================ Edit Attribute =========================

        public ActionResult EditAttribute(int attributeID)
        {
            AttributeSuggest attributeSuggest = db.AttributeSuggest.Find(attributeID);
            if (attributeSuggest == null)
            {
                return HttpNotFound();
            }
            return View(attributeSuggest);
        }

        [HttpPost]
        public ActionResult EditAttribute(AttributeSuggest attributeSuggest)
        {
            if (ModelState.IsValid)
            {
                db.Entry(attributeSuggest).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("ItemManager");
            }
            else
            {
                return View(attributeSuggest);
            }
        }

        //=========================== Delete Attribute=======================
        public ActionResult DeleteAttribute(int ID)
        {
            itemManager.deleteAttribute(ID);
            return RedirectToAction("AttributeManager");
        }
        //=========================== Location Manager======================
        public ActionResult LocationManager()
        {
            List<LocationSuggest> list = new List<LocationSuggest>();
            list = db.LocationSuggest.ToList();
            list.OrderBy(l => l.Name);
            ViewBag.listLocation = list;
            return View();
        }


        //============================= Create Location ====================
        public ActionResult CreateLocation(LocationSuggest location)
        {
            db.LocationSuggest.Add(new LocationSuggest { Name = location.Name });
            db.SaveChanges();
            return RedirectToAction("LocationManager");
        }
        //============================ Delete Location =====================
        public ActionResult DeleteLocation(int locationID)
        {

            try
            {
                var location = db.LocationSuggest.Find(locationID);
                if (location == null)
                {
                    return HttpNotFound();
                }
                db.LocationSuggest.Remove(location);
                db.SaveChanges();
                return RedirectToAction("LocationManager");
            }
            catch
            {
                return RedirectToAction("Error Page");
            }

        }


    }
}
